package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.response.ResponsePage;
import com.example.demo.entity.Menu;
import com.example.demo.entity.RoleMenuPermissionDto;
import com.example.demo.entity.User;
import com.example.demo.entity.VO.Announcement;
import com.example.demo.entity.param.AnnouncementParam;
import com.example.demo.entity.param.UserMenuParam;
import com.example.demo.exception.Asserts;
import com.example.demo.mapper.AdminMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.util.AsyncUtil;
import com.example.demo.util.JwtUtil;
import com.example.demo.util.MemberUtil;
import com.example.demo.util.YunOxTools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.lang.Assert;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<RoleMenuPermissionDto> getMenuRole() {
        Integer roleId = userMapper.getUserRoleId(MemberUtil.getUserId());
        List<RoleMenuPermissionDto> menuList = userMapper.getMenuRoleByRoleId(roleId);
        return menuList;
    }

    @Override
    public List<RoleMenuPermissionDto> getAllMenu() {
        return userMapper.getAllMenu();
    }

    @Override
    public void updateRoleMenu(UserMenuParam menuParam) {
        List<Menu> menuList = userMapper.getTotalMenu();
        userMapper.deleteMenu(menuParam.getRoleId());
        List<Menu> menus = new ArrayList<>();
        for (Menu menu : menuList) {
            for (String path : menuParam.getMenuList()) {
                if (menu.getPath().equals(path)) {
                    menu.setId(menuParam.getRoleId());
                    menus.add(menu);
                }
            }
        }
        userMapper.addMenu(menus);
    }

    @SneakyThrows
    @Override
    public void updateUserInfo(User user) {
        System.out.println(user);
        if (user.getPassword() != null) {
            user.setPassword(YunOxTools.encrypt(user.getPassword()));
        }
        userMapper.updateUserInfo(user);
    }

    @Override
    public ResponsePage<Announcement> getAnnouncementList(AnnouncementParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Announcement> announcements = userMapper.getAnnouncementList(param);
        return ResponsePage.restPage(new PageInfo<>(announcements));
    }

    @Override
    public List<Menu> getMenuList(Integer role) {
        return userMapper.getMenuList(role);
    }

    @SneakyThrows
    @Override
    public String login(User loginInfo) {
        User user = userMapper.selectUser(loginInfo);
        if (YunOxTools.isEmpty(user)) {
            Asserts.fail("此用户不存在");
        }
        if (!YunOxTools.decrypt(user.getPassword()).equals(loginInfo.getPassword())) {
            Asserts.fail("密码不正确");
        }
        return JwtUtil.generateMemberToken(user);
    }

    public static void main(String[] args) throws Exception {
        //这一段给你看这方法怎么用的
        String word = "草莓屁屁 Do vis";
        String word2 = "草莓屁屁 Do vis";
        String password = YunOxTools.encrypt(word, "caomeipipiDoVis1");
        String password2 = YunOxTools.encrypt(word2, "1234567891234567");
        System.out.println("我是password，原密码:" + word + "加密后：" + password);
        System.out.println("我是password2，原密码:" + word2 + "加密后：" + password2);
        String decyrptInfo = YunOxTools.decrypt(password, "caomeipipiDoVis1");
        String decyrptInfo2 = YunOxTools.decrypt(password2, "1234567891234567");
        System.out.println("我是加密的" + password + "，解密后：" + decyrptInfo);
        System.out.println("我是加密的" + password2 + "，解密后：" + decyrptInfo2);


        //我来说一下这一段代码
        //sout打印不用说了吧
        //YunOxTools.decrypt();这是一个方法，是我的解密方法，点进去看
        //LachesismStorm24 这个就是我得秘钥，18位，你自己后面改成你设计的密码，比如1234567890123456789
    }

    @SneakyThrows
    @Override
    public String register(User user) {
        User registerUser = new User();
        BeanUtil.copyProperties(user, registerUser);
        if (userMapper.checkUserIsExist(user.getUsername()) > 0) {
            Asserts.fail("此用户已存在");
        }
        user.setPassword(YunOxTools.encrypt(user.getPassword()));
        //这一段，就是反过来，给User的password赋值，用YunOxTools.encrypt这个方法，这个方法就是加密方法，跟刚才的正好对应
        //然后还有一个两个同名方法，直接传密码进去的，比如：
        userMapper.register(user);
        addRole(user.getId(), 2);
        return login(registerUser);
    }

    private void addRole(Long userId, Integer roleId) {
        userMapper.addRole(userId, roleId);
    }

    @Override
    public ResponsePage<User> getUserList(Integer pageNum, Integer pageSize, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> user = userMapper.getUserList(type);
        return ResponsePage.restPage(new PageInfo<>(user));
    }

    @SneakyThrows
    @Override
    public void addUser(User user) {
        user.setPassword(YunOxTools.encrypt(user.getPhone()));//初始密码为用户手机号
        userMapper.addUser(user);
        addRole(user.getId(), user.getType());
    }

    @Override
    public void setRoleById(Long userId) {
        userMapper.setRoleById(userId);
    }

    @SneakyThrows
    @Override
    public void batchAddUser() {
        List<User> userList = new ArrayList<>();
        long phoneNumber = 19100000000L; // 起始手机号
        for (int i = 0; i < 100; i++) { // 100 轮
            User user = new User();
            user.setPhone(String.valueOf(phoneNumber)); // 递增手机号
            user.setUsername("User" + phoneNumber); // 示例用户名
            user.setPassword(YunOxTools.encrypt(String.valueOf(phoneNumber))); // 默认加密密码
            user.setGender(i / 2 == 0 ? 0 : 1);
            user.setId(phoneNumber);
            userList.add(user);
            phoneNumber++;
        }
        userMapper.batchInsert(userList);
        userMapper.batchInsertRole(userList);
        userList.clear();

    }

    @Override
    public void batchDelete(List<Long> userlist) {
        System.out.println(userlist);
        userMapper.batchDelete(userlist);
    }
}

